/*
 * ReverseCommandFactory.cpp
 *
 *  Created on: Nov 21, 2008
 *      Author: Ryan
 */

#include "ReverseCommandFactory.h"

ReverseCommandFactory::ReverseCommandFactory()
{
}

ReverseCommandFactory::~ReverseCommandFactory()
{
}

OOInterface::ConstCommandPtr ReverseCommandFactory::createCommandObject( const ::Command& cmd, const OOInterface::UnitSet& allUnits, const OOInterface::UnitSet& prototypes, OOInterface::CommandFactory& factory )
{
	switch( cmd.id )
	{
	case CMD_ATTACK:
	{
		int unitID = cmd.params.at(0);

		return factory.createAttackCommand( findUnitByID( allUnits, unitID ) );
	}
		break;
	case CMD_CAPTURE:
	{
		int unitID = cmd.params.at(0);

		return factory.createCaptureCommand( findUnitByID( allUnits, unitID ) );
	}
	break;
	case CMD_GUARD:
	{
		int unitID = cmd.params.at(0);

		return factory.createGuardCommand( findUnitByID( allUnits, unitID ) );
	}
	break;
	case CMD_LOAD_UNITS:
	{
		int unitID = cmd.params.at(0);

		return factory.createLoadCommand( findUnitByID( allUnits, unitID ) );
	}
	break;
	case CMD_RECLAIM:
	{
		int unitID = cmd.params.at(0);

		return factory.createReclaimCommand( findUnitByID( allUnits, unitID ) );
	}
	break;
	case CMD_REPAIR:
	{
		int unitID = cmd.params.at(0);

		return factory.createRepairCommand( findUnitByID( allUnits, unitID ) );
	}
	break;
	case CMD_MOVE:
	{
		float x = cmd.params.at(0);
		float y = cmd.params.at(1);
		float z = cmd.params.at(2);

		OOInterface::Point3f loc(x,y,z);
		return factory.createMoveCommand( loc );
	}
	break;
	case CMD_PATROL:
	{
		float x = cmd.params.at(0);
		float y = cmd.params.at(1);
		float z = cmd.params.at(2);

		OOInterface::Point3f loc(x,y,z);
		return factory.createPatrolCommand( loc );
	}
	break;
	case CMD_UNLOAD_UNITS:
	{
		float x = cmd.params.at(0);
		float y = cmd.params.at(1);
		float z = cmd.params.at(2);

		OOInterface::Point3f loc(x,y,z);
		return factory.createUnloadCommand( loc );
	}
	break;
	case CMD_STOP:
	{
		return factory.createStopCommand();
	}
	break;

	default:
	{
		//This is most likely a Build or BuiltUnit Command

		if( cmd.id < 0 ) // Both of which have negative command IDs
		{
			if( cmd.params.size() == 4)
			{ //Build Commands have (x,y,z) and facing
				float x = cmd.params.at(0);
				float y = cmd.params.at(1);
				float z = cmd.params.at(2);

				OOInterface::Point3f loc(x,y,z);
				return factory.createBuildCommand( findUnitByID( prototypes, -1* cmd.id ), loc );
			}
			else if( cmd.params.size() == 0 )
			{
				return factory.createBuildUnitCommand( findUnitByID( prototypes, -1* cmd.id ) );
			}
			else
			{
				return factory.createStopCommand(); //Defaultly return a stop command if its not one of those.
			}
		}
		else
		{
			return factory.createStopCommand(); //Defaultly return a stop command if its not one of those.
		}
	}
	}
}

OOInterface::ConstUnitPtr ReverseCommandFactory::findUnitByID( const OOInterface::UnitSet& allUnits, int id )
{
	for( OOInterface::UnitSet::const_iterator unit = allUnits.begin(); unit != allUnits.end(); ++unit )
	{
		if( (*unit)->getID() == id )
			return (*unit);
	}

	return OOInterface::ConstUnitPtr(); //Shouldn't be able to get this far.
}
